package club.service.impl;

import club.dao.AnswerMapper;
import club.dao.CommentMapper;
import club.dao.UserMapper;
import club.pojo.Answer;
import club.pojo.Comment;
import club.pojo.User;
import club.service.AnswerService;
import club.service.UserService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
@Service
@Transactional
public class AnswerServiceImpl implements AnswerService {

    @Resource
    private AnswerMapper answerMapper;

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private UserMapper userMapper;

    /**
     * 查询某个评论下的所有回复
     * @param commentId 评论ID
     * @return 包含回复信息的列表
     */
    @Override
    public List<Answer> answersAboutOneComment(Integer commentId) {
        // 构建查询条件
        EntityWrapper wrapper = new EntityWrapper();
        if (commentId != null) {
            wrapper.eq("commentId", commentId);
        }
        // 查询符合条件的回复列表
        List<Answer> list = answerMapper.selectList(wrapper);
        if (!list.isEmpty()) {
            // 如果回复列表不为空，为每个回复关联对应的用户信息和可能的被回复的回复信息
            for (Answer answer : list) {
                // 关联回复的用户信息
                User user = userMapper.selectById(answer.getUserId());
                answer.setUser(user);
                // 如果该回复是对另一个回复的回复，则查询被回复的回复信息和对应的用户信息
                if (answer.getReplayId() != null) {
                    Answer repliedAnswer = answerMapper.selectById(answer.getReplayId());
                    User repliedUser = userMapper.selectById(repliedAnswer.getUserId());
                    repliedAnswer.setUser(repliedUser);
                    // 将被回复的回复信息设置到当前回复对象中
                    answer.setAnswer(repliedAnswer);
                }
            }
            return list;
        } else {
            // 如果回复列表为空，返回null
            return null;
        }
    }

    /**
     * 创建新的回复
     * @param userId 回复用户的ID
     * @param commentId 回复所属评论的ID
     * @param content 回复内容
     * @return 插入操作影响的行数，通常为1
     */
    @Override
    public Integer create(Integer userId, Integer commentId, String content) {
        // 创建回复对象并设置相关信息
        Answer answer = new Answer();
        answer.setUserId(userId);
        answer.setCommentId(commentId);
        answer.setContent(content);
        answer.setAnswerTime(new Date());
        // 执行插入操作
        return answerMapper.insert(answer);
    }

    /**
     * 根据ID查询回复信息，并关联其所属评论信息
     * @param id 回复的ID
     * @return 包含回复信息及其所属评论信息的回复对象
     */
    @Override
    public Answer findById(Integer id) {
        // 根据ID查询回复信息
        Answer answer = answerMapper.selectById(id);
        // 查询回复所属的评论信息
        Comment comment = commentMapper.selectById(answer.getCommentId());
        answer.setComment(comment);
        return answer;
    }

    /**
     * 创建新的回复（针对某个回复的回复）
     * @param userId 回复用户的ID
     * @param replayId 被回复的回复的ID
     * @param content 回复内容
     * @param commentId 回复所属评论的ID
     * @return 插入操作影响的行数，通常为1
     */
    @Override
    public Integer creates(Integer userId, Integer replayId, String content, Integer commentId) {
        // 创建回复对象并设置相关信息
        Answer answer = new Answer();
        answer.setUserId(userId);
        answer.setReplayId(replayId);
        answer.setCommentId(commentId);
        answer.setContent(content);
        answer.setAnswerTime(new Date());
        // 执行插入操作
        return answerMapper.insert(answer);
    }

}